<?php
namespace App\Http\Controllers\Admin;

use App\Exceptions\ExceptionSms;
use App\Http\Controllers\Controller;
use App\Models\Label;
use Carbon\Carbon;
use Illuminate\Http\Request;

class CommonController extends Controller
{
    protected $model;

    const LimitMinValue = 10;
    const LimitMaxValue = 100;

    /**
     * @var Request
     */
    protected $request;
    /**
     * @var mixed
     */
    protected $limit;

    /**
     * 构造函数
     * CommonController constructor.
     * @param Request $request
     */
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->limit   = $request->get('limit') ?? self::LimitMinValue;
    }

    /**
     * 接收请求参数
     * @param string|null $paramKey
     * @return array|mixed
     * @name:input
     * @author: lisa
     * @datetime: 2021/10/12 10:09
     */
    public function input(string $paramKey = null)
    {
        if (!$paramKey) return $this->request->all();
        return $this->request->get($paramKey);
    }

    // 删除
    public function deletes()
    {
        ExceptionSms::checkParams($this->input(), [
            'ids' => 'required|array'
        ]);
        $ids = $this->input('ids') ?? [];
        if (!is_array($ids)) ExceptionSms::returnError('请求参数错误');
        if (!collect($ids)->count()) ExceptionSms::returnError('请勾选数据');
        $this->model::whereIds($ids)->update([
            'is_delete'  => 1,
            'updated_at' => Carbon::now()->toDateTimeString()
        ]);
        return ExceptionSms::returnSuccess();
    }

    // 启用
    public function enables()
    {
        ExceptionSms::checkParams($this->input(), [
            'ids' => 'required|array'
        ]);
        $ids = $this->input('ids') ?? [];
        if (!is_array($ids)) ExceptionSms::returnError('请求参数错误');
        if (!collect($ids)->count()) ExceptionSms::returnError('请勾选数据');
        $this->model::whereIds($ids)->update([
            'is_enable'  => 1,
            'updated_at' => Carbon::now()->toDateTimeString()
        ]);
        return ExceptionSms::returnSuccess();
    }

    // 禁用
    public function disables()
    {
        ExceptionSms::checkParams($this->input(), [
            'ids' => 'required|array'
        ]);
        $ids = $this->input('ids') ?? [];
        if (!is_array($ids)) ExceptionSms::returnError('请求参数错误');
        if (!collect($ids)->count()) ExceptionSms::returnError('请勾选数据');
        $this->model::whereIds($ids)->update([
            'is_enable'  => 0,
            'updated_at' => Carbon::now()->toDateTimeString()
        ]);
        return ExceptionSms::returnSuccess();
    }
}
